//
//  WYCRegisterController.m
//  51信用卡
//
//  Created by 中企互联 on 2017/11/3.
//  Copyright © 2017年 中企互联. All rights reserved.
//

#import "WYCRegisterController.h"
#import "UIButton+CountDown.h"
#import "WYCRealnameController.h"
#import "RSAUtil.h"
#import "QRScanViewController.h"
@interface WYCRegisterController ()<QRScanDelegate>

/** 账号输入框*/
@property(nonatomic,strong)UITextField * UserTextField;

/** 密码*/
@property(nonatomic,strong)UITextField * passtexfield;


/** 再次输入密码*/
@property(nonatomic,strong)UITextField * Zpasstexfield;

/** 验证码*/
@property(nonatomic,strong)UITextField * codeTexField;


/** 邀请码*/
@property(nonatomic,strong)UITextField * Rgscodefield;

/** 密码隐藏或显示*/
@property(nonatomic,strong)UIButton * ZpasstexfieldButton;

/** 密码隐藏或显示*/
@property(nonatomic,strong)UIButton * passtexfieldButton;

@end

@implementation WYCRegisterController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //创建界面
    [self setupCreat];
    
}
#pragma make ------->>创建界面
- (void)setupCreat{
    
    self.view.backgroundColor = wycwhiteColor;
    
    
    UIView * registerView = [[UIView alloc] init];
    registerView.frame = CGRectMake(0, 0, ScreenWidth, 64);
    registerView.backgroundColor = wycMain;
    [self.view addSubview:registerView];
    
    
    //不是Nav。所以自己写的View和label
    UILabel * NavRigsterLabel = [[UILabel alloc] init];
    NavRigsterLabel.frame = CGRectMake(ScreenWidth / 2 - 100 * px, 30, 200 * px, 68 * px);
    NavRigsterLabel.textColor = [UIColor whiteColor];
    //    NavRigsterLabel.font = [UIFont systemFontOfSize:BigFont];
    NavRigsterLabel.font = [UIFont boldSystemFontOfSize:BigFont];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    NavRigsterLabel.text = @"注册";
    [registerView addSubview:NavRigsterLabel];
    
    
    
    //返回按钮
    UIButton *  backbutton = [[UIButton alloc] init];
    [backbutton setImage:[UIImage imageNamed:@"Nav_back"] forState:UIControlStateNormal];
    [backbutton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    backbutton.frame = CGRectMake(30 * px, NavRigsterLabel.y, 66 * px , 66 * px);
    [registerView addSubview:backbutton];
    
    
    ////////////////
    //账号输入框/////
    ///////////////
    UILabel * chainalabel = [[UILabel alloc] init];
    chainalabel.frame = CGRectMake(80 * px, CGRectGetMaxY(registerView.frame) + 80 * px, 150 * px, 120 * px);
    chainalabel.textColor = [UIColor grayColor];
    chainalabel.text = @"+86";
    [self.view addSubview:chainalabel];
    
    UITextField * usertexfield = [[UITextField alloc] init];
    usertexfield.frame = CGRectMake(CGRectGetMaxX(chainalabel.frame),CGRectGetMaxY(registerView.frame) + 80 * px, ScreenWidth - WYCcellHeight, 120 * px);
    //    usertexfield.backgroundColor = [UIColor redColor];
    usertexfield.placeholder = @"请输入手机号";
    usertexfield.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:usertexfield];
    
    self.UserTextField = usertexfield;
    
    UIButton * usertexfieldButton = [[UIButton alloc] init];
    usertexfieldButton.frame = CGRectMake(usertexfield.width - 40 * px, usertexfield.y, 80 * px, 80 * px);
    [usertexfieldButton addTarget:self action:@selector(usertexfieldButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [usertexfieldButton setImage:[UIImage imageNamed:@"celar"] forState:UIControlStateNormal];
    [self.view addSubview:usertexfieldButton];
    
    
    //手机提示线
    UIView * lineView = [[UIView alloc] init];
    lineView.frame = CGRectMake(chainalabel.x, CGRectGetMaxY(usertexfield.frame) + 5 * px, usertexfield.width, 1);
    lineView.backgroundColor = WYCGlobalBg;
    [self.view addSubview:lineView];
    
    
    ////////////////
    /////密码输入框/////
    ///////////////
    
    
    
    
    UITextField * codefield = [[UITextField alloc] init];
    codefield.frame = CGRectMake(chainalabel.x, CGRectGetMaxY(lineView.frame) + 80 * px, usertexfield.width / 2, usertexfield.height);
    codefield.placeholder = @"验证码";
    codefield.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:codefield];
    _codeTexField = codefield;
    
    //手机提示线
    UIView * passlineView = [[UIView alloc] init];
    passlineView.frame = CGRectMake(chainalabel.x, CGRectGetMaxY(codefield.frame) + 5 * px, lineView.width, 1);
    passlineView.backgroundColor = WYCGlobalBg;
    [self.view addSubview:passlineView];
    
    //获取验证码
    UIButton * codebutton = [[UIButton alloc] init];
    codebutton.frame = CGRectMake(ScreenWidth - 350 * px, codefield.y, 300 * px, codefield.height);
    [codebutton setTitleColor:wycMain forState:UIControlStateNormal];
    codebutton.titleLabel.font = [UIFont systemFontOfSize:SmallFont];
    [codebutton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [codebutton addTarget:self action:@selector(countDownBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:codebutton];
    
    
    
    ////////////////
    //密码输入框/////
    ///////////////
    
    
    UITextField * passtexfield = [[UITextField alloc] init];
    passtexfield.frame = CGRectMake(chainalabel.x, CGRectGetMaxY(passlineView.frame) + 80 * px, usertexfield.width, usertexfield.height);
    passtexfield.placeholder = @"请输入密码";
    passtexfield.secureTextEntry = YES;
    [self.view addSubview:passtexfield];
    self.passtexfield = passtexfield;
    
    UIButton * passtexfieldButton = [[UIButton alloc] init];
    passtexfieldButton.frame = CGRectMake(passtexfield.width - 40 * px, passtexfield.y, 80 * px, 80 * px);
    [passtexfieldButton addTarget:self action:@selector(passtexfieldButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [passtexfieldButton setImage:[UIImage imageNamed:@"隐藏"] forState:UIControlStateNormal];
    [passtexfieldButton setImage:[UIImage imageNamed:@"展示"] forState:UIControlStateSelected];
    [self.view addSubview:passtexfieldButton];
    self.passtexfieldButton = passtexfieldButton;
    
    
    //手机提示线
    UIView * passView = [[UIView alloc] init];
    passView.frame = CGRectMake(chainalabel.x, CGRectGetMaxY(passtexfield.frame) + 5 * px, usertexfield.width, 1);
    passView.backgroundColor = WYCGlobalBg;
    [self.view addSubview:passView];
    
    
    
    
    
    /************再次输入密码************/
    
    
    UITextField * Zpasstexfield = [[UITextField alloc] init];
    Zpasstexfield.frame = CGRectMake(chainalabel.x, CGRectGetMaxY(passView.frame)+80 * px, usertexfield.width, usertexfield.height);
    Zpasstexfield.placeholder = @"再次输入密码";
    Zpasstexfield.secureTextEntry = YES;
    [self.view addSubview:Zpasstexfield];
    self.Zpasstexfield = Zpasstexfield;
    
    
    UIButton * ZpasstexfieldButton = [[UIButton alloc] init];
    ZpasstexfieldButton.frame = CGRectMake(Zpasstexfield.width - 40 * px, Zpasstexfield.y, 80 * px, 80 * px);
    [ZpasstexfieldButton addTarget:self action:@selector(ZpasstexfieldButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [ZpasstexfieldButton setImage:[UIImage imageNamed:@"隐藏"] forState:UIControlStateNormal];
    [ZpasstexfieldButton setImage:[UIImage imageNamed:@"展示"] forState:UIControlStateSelected];
    [self.view addSubview:ZpasstexfieldButton];
    self.ZpasstexfieldButton = ZpasstexfieldButton;
    //手机提示线
    UIView * ZpassView = [[UIView alloc] init];
    ZpassView.frame = CGRectMake(chainalabel.x, CGRectGetMaxY(Zpasstexfield.frame) + 5 * px, usertexfield.width, 1);
    ZpassView.backgroundColor = WYCGlobalBg;
    [self.view addSubview:ZpassView];
    
    
    /************再次输入密码************/
    
    
    UITextField * Rgscodefield = [[UITextField alloc] init];
    Rgscodefield.frame = CGRectMake(chainalabel.x, CGRectGetMaxY(ZpassView.frame)+80 * px, usertexfield.width, usertexfield.height);
    Rgscodefield.placeholder = @"请输入注册码";
//    Rgscodefield.secureTextEntry = YES;
    [self.view addSubview:Rgscodefield];
//    self.Zpasstexfield = Rgscodefield;
    self.Rgscodefield = Rgscodefield;
    
    
    UIButton * RgscodefieldButton = [[UIButton alloc] init];
    RgscodefieldButton.frame = CGRectMake(Rgscodefield.width - 40 * px, Rgscodefield.y, 80 * px, 80 * px);
    [RgscodefieldButton addTarget:self action:@selector(rgscodeButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [RgscodefieldButton setImage:[UIImage imageNamed:@"scanning"] forState:UIControlStateNormal];
    [self.view addSubview:RgscodefieldButton];
    
    //手机提示线
    UIView * RgscodeView = [[UIView alloc] init];
    RgscodeView.frame = CGRectMake(chainalabel.x, CGRectGetMaxY(Rgscodefield.frame) + 5 * px, usertexfield.width, 1);
    RgscodeView.backgroundColor = WYCGlobalBg;
    [self.view addSubview:RgscodeView];
    
    
    
    //注册按钮
    UIButton * loginButton = [[UIButton alloc] init];
    loginButton.frame = CGRectMake(80 * px, CGRectGetMaxY(RgscodeView.frame) + 200 * px, usertexfield.width, 150 * px);
    loginButton.backgroundColor = wycMain;
    loginButton.layer.cornerRadius = 3;
    [loginButton setTitle:@"注册" forState:UIControlStateNormal];
    loginButton.titleLabel.font = [UIFont systemFontOfSize:72 * px];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(RegisterClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginButton];
    
    
    
    UILabel * PromptLable = [[UILabel alloc] init];
    UIFont *fnt = [UIFont fontWithName:@"HelveticaNeue" size:SmallFont];
    NSString * str = @"点击《注册》表示您同意";
    PromptLable.font = fnt;
    PromptLable.x = loginButton.x + 120 * px;
    PromptLable.y = CGRectGetMaxY(loginButton.frame) + 30 * px;
    CGSize PromptLableSize = [str sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil]];
    PromptLable.text = str;
    PromptLable.textColor = WYCRGBColor(157, 157, 157);
    PromptLable.frame = CGRectMake(PromptLable.x, PromptLable.y, PromptLableSize.width, PromptLableSize.height);
    [self.view addSubview:PromptLable];
    
    
    
    //用户须知的按钮
    UIButton * userAgreement = [[UIButton alloc] init];
    [userAgreement setTitle:@"【用户注册协议】" forState:UIControlStateNormal];
    userAgreement.titleLabel.font = [UIFont systemFontOfSize:43 * px];
    userAgreement.frame = CGRectMake(CGRectGetMaxX(PromptLable.frame), PromptLable.y, PromptLable.width + 90 * px, PromptLable.height);
    userAgreement.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [userAgreement setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.view addSubview:userAgreement];
}
//键盘回收
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_UserTextField resignFirstResponder];
    [_codeTexField resignFirstResponder];
    [_passtexfield resignFirstResponder];
    [_Zpasstexfield resignFirstResponder];
}
#pragma make ------>>获取验证码
- (void)countDownBtnAction:(UIButton *)button
{
    WYCLog(@"----------%@",self.UserTextField.text);
    WYCLogFunc;
    if (self.UserTextField.text.length == 0) {
        
        NSString * showStr = @"手机号不能为空";
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:showStr];
        return;
    }else{
        [button startWithTime:59 title:@"点击重新获取" countDownTitle:@"s后重新获取" mainColor:[UIColor colorWithRed:0 / 255.0 green:0 / 255.0 blue:0 / 255.0 alpha:0] countColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0]];
        NSString * httpsStr = [NSString stringWithFormat:@"%@mobile=%@&type=%@",Rcode,self.UserTextField.text,Ucodekey];
        
        [HttpTool POST:httpsStr params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
            
            if ([[responseObject objectForKey:@"code"] intValue] == 0) {
                
                [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
                
            }else{
                
                [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
            }
            
        } fail:^(NSURLSessionDataTask *task, NSError *error) {
            
        }];
        
    }
}

#pragma make ------>>注册按钮
- (void)RegisterClick {
    WYCLogFunc;
   // 手机号判断是否为空
    if(self.UserTextField.text.length == 0)
    {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"手机号不能为空"];
        return;
    }
    //验证码判断是否为空
    if(self.codeTexField.text.length == 0)
    {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"请输入验证码"];
        return;
    }
    //手机号判断是否为空
    if(self.passtexfield.text.length == 0)
    {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"密码不能为空"];
        return;
    }
    //密码判断是否为空
    NSString * passtexfieldStr = self.passtexfield.text;
    NSString * ZpassfieldStr = self.Zpasstexfield.text;
    if(![passtexfieldStr isEqualToString:ZpassfieldStr])
    {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"两次输入密码不一致"];
        return;
    }
    if (self.Rgscodefield.text.length == 0) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"邀请码不能为空"];
        return;
    }
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
        NSString * passStr = [NSString stringWithFormat:@"%@",self.passtexfield.text];
        NSString *pas = [RSAUtil encryptString:passStr publicKey:RSA_P_KEY];
    //
        params[@"mobile"] = self.UserTextField.text;
        params[@"password"] = pas;
        params[@"authcode"] = self.codeTexField.text;
        params[@"regCode"] = self.Rgscodefield.text;
        [HttpTool POST:Register params:params success:^(NSURLSessionDataTask *task, id responseObject) {
            if ([[responseObject objectForKey:@"code"] intValue] == 0) {
                NSDictionary * dict = [responseObject objectForKey:@"result"];
                WYCRealnameController * authen = [[WYCRealnameController alloc] init];
                [self presentViewController:authen animated:NO completion:nil];
                WYCAccount * Account = [[WYCAccount alloc] initWithDic:dict];

                [WYCAccountTool saveAccount:Account];

                [WYCIsAppLoginTool saveIsAppLogin:YES];
            }else{

                [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
            }

        } fail:^(NSURLSessionDataTask *task, NSError *error) {
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:Failed];
        }];
    
}
- (void)rgscodeButtonClick{
    WYCLogFunc;
    QRScanViewController *vc = [[QRScanViewController alloc] init];
    vc.delegate = self;
    vc.hidesBottomBarWhenPushed = YES;
    [self showViewController:vc sender:nil];
    
}
- (void)passtexfieldButtonClick{
    self.passtexfieldButton.selected = !self.passtexfieldButton.selected;
    if (self.passtexfieldButton.selected == YES) {
        self.passtexfield.secureTextEntry = NO;
    }else{
        
        self.passtexfield.secureTextEntry = YES;
    }
    
}

- (void)ZpasstexfieldButtonClick{
    self.ZpasstexfieldButton.selected = !self.ZpasstexfieldButton.selected;
    if (self.ZpasstexfieldButton.selected == YES) {
        self.Zpasstexfield.secureTextEntry = NO;
    }else{
        
        self.Zpasstexfield.secureTextEntry = YES;
    }
    
}
- (void)qrScanResult:(NSString *)result viewController:(QRScanViewController *)qrScanVC
{
    
    [qrScanVC dismissViewControllerAnimated:NO completion:nil];
    
    self.Rgscodefield.text = result;
}
- (void)usertexfieldButtonClick{
    
    self.UserTextField.text = @"";
}
#pragma make ------>>返回按钮
- (void)backClick{
    [self dismissViewControllerAnimated:NO completion:nil];
    
}

@end
